package com.ys.otms.controller;

import com.ys.otms.services.IPayServices;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2018/9/18.
 */
@Controller
@RequestMapping("/pay")
public class PayController {

    private static Logger logger = LoggerFactory.getLogger(PayController.class);

    @Autowired
    private IPayServices iPayServices;

    /**
     * 微信支付
     * @return
     * @throws Exception
     */
    @RequestMapping("/toWXPay")
    @ResponseBody
    public JSONObject toWXPay(HttpServletRequest request, HttpServletResponse response)throws Exception{
        logger.info("PayController toWXpay start");
        JSONObject jsonWxPay = new JSONObject();
        try {
            jsonWxPay = iPayServices.toWXPay(request,response);
        } catch (Exception e) {
            logger.error("PayController toWXpay Exception",e);
            jsonWxPay .put("SUCCESS",false);
            jsonWxPay.put("MSG","查询失败!");
        }
        return jsonWxPay;
    }

    /**
     * 微信支付回调函数
     * @return
     * @throws Exception
     */
    @RequestMapping("/wxNotifyUrl")
    @ResponseBody
    public String wxNotifyUrl(HttpServletRequest request, HttpServletResponse response)throws Exception{
        logger.info("PayController wxNotifyUrl start");
        JSONObject jsonBack = new JSONObject();
        try {
            jsonBack = iPayServices.wxNotifyUrl(request,response);
        } catch (Exception e) {
            logger.error("PayController wxNotifyUrl Exception",e);
            jsonBack .put("SUCCESS",false);
            jsonBack.put("MSG","查询失败!");
        }
        return null;
    }

    /**
     * 查询余额
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectLeftAmount")
    @ResponseBody
    public JSONObject selectLeftAmount(HttpServletRequest request)throws Exception{
        logger.info("PayController selectLeftAmount start");
        JSONObject jsonLeftAmount = new JSONObject();
        try {
            jsonLeftAmount = iPayServices.selectLeftAmount(request);
        } catch (Exception e) {
            logger.error("PayController selectLeftAmount Exception",e);
            jsonLeftAmount.put("SUCCESS",false);
            jsonLeftAmount.put("MSG","查询数据失败！");
        }
        return jsonLeftAmount;
    }

    /**
     * 微信支付回调函数
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectWxOrder")
    @ResponseBody
    public JSONObject selectWxOrder(HttpServletRequest request)throws Exception{
        logger.info("PayController selectWxOrder start");
        JSONObject jsonOrder = new JSONObject();
        try {
            jsonOrder = iPayServices.selectWxOrder(request);
        } catch (Exception e) {
            logger.error("PayController selectWxOrder Exception",e);
            jsonOrder .put("SUCCESS",false);
            jsonOrder.put("MSG","查询失败!");
        }
        return jsonOrder;
    }
}
